package org.lq.shopping.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.lq.shopping.dao.Shop_collectionDao;
import org.lq.shopping.dao.impl.Shop_collectionDaoImpl;
import org.lq.shopping.entity.Shop_collection;
import org.lq.shopping.service.Shop_collectionService;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class Shop_collectionServiceImpl implements Shop_collectionService {
	Shop_collectionDao dao = new Shop_collectionDaoImpl();
	@Override
	public boolean deleteCollection(int id) {
		
		return dao.delete(id)>0;
	}

	@Override
	public List<Shop_collection> findPage(int userId, int pageIndex, int pageSize) {
JedisPoolConfig config = new JedisPoolConfig();
		
		
		config.setMaxTotal(50);
		
		config.setMaxIdle(5);
		
		config.setMaxWaitMillis(1000*100);
		
		config.setTestOnBorrow(true);
		
		config.setTestOnReturn(true);
		
	
		JedisPool pool  =   new JedisPool(config,"127.0.0.1",6379,2000);
	
		Jedis jedis = pool.getResource();
		Gson gson = new Gson();
		List<Shop_collection> list = dao.findbyuserid(userId);
		for(Shop_collection s :list) {
			String s3 =gson.toJson(s);
			jedis.lpush("red", s3);
		}
		
		List<String> newlist = jedis.lrange("red", (pageIndex-1)*pageSize,pageIndex*pageSize);
		List<Shop_collection> fenye = new  ArrayList<>();
		
		
		for(String se : newlist) {
			Shop_collection s2 = gson.fromJson(se,Shop_collection.class);
			
			fenye.add(s2);
		
		
			
		}
		
		return fenye;
	}




}
